package com.fare.taluo.action.system;

import java.util.Locale;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.system.auth.UserInfoLogApplication;
import com.fare.taluo.auth.security.ContextLoginUser;
import com.fare.taluo.auth.security.LogonUserCertificate;
import com.fare.taluo.util.excel.util.StringUtils;
import com.opensymphony.xwork2.ActionContext;

@Controller("LoginAction")
@Scope("prototype")
@Namespace("/system/start")
public class StartAction extends BaseFdcAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5991769771905114365L;
	@Resource(name ="userInfoLogApplication")
	private UserInfoLogApplication userInfoLogApplication; 
	/**
	 * 进入首页
	 * @return
	 */
	@Action(value = "index", results = {@Result(name = "success", type="dispatcher", location="/index.jsp") })
	public String index() {
		
		return SUCCESS;
	}
	/**
	 * 进入登陆页
	 * @return
	 */
	@Action(value = "logon", results = {@Result(name = "success", type="dispatcher", location="/login.jsp") })
	public String logon() {
		//在此处释放掉session，记录当前日期
		try {
			Locale locale = super.getLocale();
			//当前用户信息
			try{
				LogonUserCertificate user=new LogonUserCertificate();
				user = ContextLoginUser.getLoginUser();
				userInfoLogApplication.updateVo(user);
			}catch (Exception e) {
				request.getSession().invalidate();
			}
			//保存信息后，清理session
//			request.getParameter("language");
			request.getSession().invalidate();
			request.getSession().setAttribute("WW_TRANS_I18N_LOCALE", locale);
			if(locale !=null){
				if(locale.toString().equalsIgnoreCase("en_US")){
					request.getSession().setAttribute("currentLanguage", "english");
				}else{
					request.getSession().setAttribute("currentLanguage", "china");
				}
				ServletActionContext.getContext().getValueStack().setValue(ActionContext.LOCALE, "en_US");
			}else{
				ServletActionContext.getContext().getValueStack().setValue(ActionContext.LOCALE, "zh_CN");
			}
			//System.out.println("+++++++"+super.getLocale()+"---------"+request.getParameter("language"));
			
			super.execute();
		} catch (Exception e) {
		}
		
		return SUCCESS;
	}
	/**
	 * 进入体验版本
	 * @return
	 */
	@Action(value = "experience", results = {@Result(name = "success", type="dispatcher", location="/pages/system/start/experience.jsp") })
	public String experience() {
		return SUCCESS;
	}
	/**
	 * 进入服务条款
	 * @return
	 */
	@Action(value = "termsService", results = {@Result(name = "en", type="dispatcher", location="/pages/system/start/en/terms_service.jsp"),
			@Result(name = "zh", type="dispatcher", location="/pages/system/start/zh/terms_service.jsp")})
	public String termsService() {
		String language = super.getLocale().getLanguage();
		if(StringUtils.isEmpty(language) || language.equalsIgnoreCase("zh"))
			return "zh";
		else
			return "en";
	}
	/**
	 * 进入法律声明
	 * @return
	 */
	@Action(value = "legalNotice", results = {@Result(name = "en", type="dispatcher", location="/pages/system/start/en/legal_notice.jsp"),
			@Result(name = "zh", type="dispatcher", location="/pages/system/start/zh/legal_notice.jsp")})
	public String legalNotice() {
		String language = super.getLocale().getLanguage();
		if(StringUtils.isEmpty(language) || language.equalsIgnoreCase("zh"))
			return "zh";
		else
			return "en";
	}
	
}
